package com.lone.smallmovie.utils;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class CindyTools {


    private static final Properties cindy = read("cindy.properties");
    private static Logger log = Logger.getLogger(CindyTools.class);


    public static String getValue(String key) {

        String data = null;
        try {
            data = new String(cindy.getProperty(key).getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.info(e);
        }

        return data;
    }

    public static boolean checkPath(String path) {
        String paths = getValue("game.allow.path");

        if (paths == null) {
            return false;
        } else {
            String []p =split(paths, ",");
            for (String pa : p){
                if (pa.equals(path)){
                    return true;
                }
            }
            return false;
        }
    }



    public static Properties read(String path) {
        Properties properties = new Properties();
        if (path != null) {
            InputStream is = CindyTools.class.getResourceAsStream(path);
            if (is == null) {
                is = CindyTools.class.getClassLoader().getResourceAsStream(path);
            }

            if (is == null) {
                try {
                    is = new FileInputStream(path);
                } catch (FileNotFoundException var15) {
                    ;
                }
            }

            if (is != null) {
                try {
                    properties.load((InputStream)is);
                } catch (IOException var13) {
                    ;
                } finally {
                    try {
                        ((InputStream)is).close();
                    } catch (IOException var12) {
                        ;
                    }

                }
            }
        }

        return properties;
    }


    public static String[] split(final String str, final String separatorChars) {
        return splitWorker(str, separatorChars, -1, false);
    }


    private static String[] splitWorker(final String str, final String separatorChars, final int max, final boolean preserveAllTokens) {
        // Performance tuned for 2.0 (JDK1.4)
        // Direct code is quicker than StringTokenizer.
        // Also, StringTokenizer uses isSpace() not isWhitespace()

        if (str == null) {
            return null;
        }
        final int len = str.length();
        if (len == 0) {
            return ArrayUtils.EMPTY_STRING_ARRAY;
        }
        final List<String> list = new ArrayList<String>();
        int sizePlus1 = 1;
        int i = 0, start = 0;
        boolean match = false;
        boolean lastMatch = false;
        if (separatorChars == null) {
            // Null separator means use whitespace
            while (i < len) {
                if (Character.isWhitespace(str.charAt(i))) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                    continue;
                }
                lastMatch = false;
                match = true;
                i++;
            }
        } else if (separatorChars.length() == 1) {
            // Optimise 1 character case
            final char sep = separatorChars.charAt(0);
            while (i < len) {
                if (str.charAt(i) == sep) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                    continue;
                }
                lastMatch = false;
                match = true;
                i++;
            }
        } else {
            // standard case
            while (i < len) {
                if (separatorChars.indexOf(str.charAt(i)) >= 0) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                    continue;
                }
                lastMatch = false;
                match = true;
                i++;
            }
        }
        if (match || preserveAllTokens && lastMatch) {
            list.add(str.substring(start, i));
        }
        return list.toArray(new String[list.size()]);
    }

}
